package lpg.tool.optionpg;

import java.io.File;
import lpg.LpgException;

public class Main
{
    public static void main(String[] args)
    {
        // If only "lpg.tool.optionpg.Main" is typed, we display the help screen.
        if (args.length == 0 || args[0].startsWith("?"))
        {
            printUsage();
            return;
        }

        File dir = new File(args[0]);
        if (dir.isFile())
        {
            System.out.println("***Error: \"" + args[0] + "\" is a file.");
            return;
        }
        File packageDir = new File(dir, "lpg");
        if (!packageDir.exists() && !packageDir.mkdirs())
        {
            System.out.println("***Error: Cannot make directory \"" + packageDir.getPath() + "\".");
            return;
        }
        File parserFile = new File(packageDir, "OptionParser.java");
        if (parserFile.isDirectory())
        {
            System.out.println("***Error: \"" + parserFile.getPath() + "\" is a directory.");
            return;
        }

        // Generate parser file
        Control control = new Control(parserFile);
        try
        {
            control.process();
        }
        catch (LpgException e)
        {
            System.out.println("***Error: " + e.getMessage());
        }
    }

    private static void printUsage()
    {
        System.out.println();
        System.out.println("Usage: lpg.tool.optionpg.Main <directory name>");
    }

}
